/*
 * Copyright 2011-2012 INSA Rennes
 * 
 * This file is part of EIImage.
 * 
 * EIImage is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * EIImage is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with EIImage.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <QApplication>
#include <QTranslator>

#include <GenericInterface.h>
#include <QSettings>
#include <iostream>
#include <QLibraryInfo>

#include "BuiltinOpSet.h"

#include "Services/PluginManager.h"
#include "Services/EIImageService.h"

#include "Operations/PointOp.h"
#include "Operations/ThresholdOp.h"
#include "Operations/TranslateOp.h"
#include "Operations/RotateOp.h"
#include "Operations/FlipOp.h"
#include "Operations/CenterOp.h"
#include "Operations/SplitColorOp.h"
#include "Operations/CombineColorOp.h"
#include "Operations/SignalToNoiseOp.h"
#include "Operations/MeanSquaredErrorOp.h"
#include "Operations/FFTOp.h"
#include "Operations/IFFTOp.h"
#include "Operations/RandomImgOp.h"
#include "Operations/NoiseOp.h"
#include "Operations/BFlitOp.h"
#include "Operations/DMMOp.h"
#include "Operations/PseudoColorOp.h"
#include "Operations/CroissanceOp.h"
#include "Operations/ZeroCrossingOp.h"
#include "Operations/HistogramOp.h"
#include "Operations/ColorimetryOp.h"
#include "Operations/SinusSynthesisOp.h"
#include "Operations/ScalingOp.h"
#include "Operations/QuantificationOp.h"
#include "Operations/EntropyOp.h"
#include "Operations/HuffmanOp.h"
#include "Operations/RejectionRingOp.h"
#include "Operations/MICDEncodingOp.h"
#include "Operations/HadamardOp.h"
#include "Operations/DCTOp.h"
#include "Operations/HoughOp.h"
#include "Operations/InverseHoughOp.h"
#include "Operations/PyramidOp.h"
#include "Operations/InversePyramidOp.h"
#include "Operations/ClassAnalysisOp.h"
#include "Operations/ClassResultOp.h"
#include "Operations/SeparatorOp.h"
#include "Operations/ParamRepOp.h"
#include "Operations/HelpOp.h"
#include "Operations/AboutOp.h"
#include "Operations/ImplementationIW.h"
#include "Operations/ApplyOpOp.h"
#include "Operations/ConvertOp.h"



#include "Language.h"
#include "ConfigApplication.h"
#include "Services/MorphoMatService.h"
#include "Services/FilteringService.h"

using namespace genericinterface;
using namespace std;

int main(int argc, char** argv)
{


  QString applicationname(QString(APP_NAME));

  QApplication app(argc, argv);


  //We add information about ther application for the OS
  app.setOrganizationDomain(QString(DOMAIN));
  app.setOrganizationName(QString(ORGANIZATION_NAME));
  app.setApplicationName(applicationname);
  app.setApplicationVersion(QString(APPVERSION));
  QSettings * settings = new QSettings(QString(ORGANIZATION_NAME),QString(APP_NAME));


  Log::configure(true, false, 0);
  QString lang = QString("en_US");
  if(argc > 1) {
    lang = QString(argv[1]);
  }

  QTranslator qtTranslator;
  QString tr = "qt_";
  tr += lang;
  if(!qtTranslator.load(tr, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
      cout << "Error while loading " << tr.toStdString() << endl;
  }
  app.installTranslator(&qtTranslator);

  QTranslator giTranslator;
  if(!giTranslator.load(QString("genericinterface_") + lang.mid(0, 2))) {
      cout << "Error while loading genericinterface_en.qm" << endl;
  }
  app.installTranslator(&giTranslator);

  QTranslator eiiTranslator;
  if(!eiiTranslator.load(QString("eiimage") + lang.mid(0, 2))) {
      cout << "Error while loading eiimage_en.qm" << endl;
  }
  app.installTranslator(&eiiTranslator);

  //Definition of language in Language.h
  if(lang.compare("en_EN")){
    settings->setValue("language", EN_GB);
  }else if(lang.compare("fr_FR")){
    settings->setValue("language", FR_FR);
  }




  GenericInterface gi(applicationname, Qt::RightDockWidgetArea);

  PluginManager* pluginManager = new PluginManager(&gi);
  EIImageService* eiimageService = new EIImageService(&gi);

//  gi.addService(eiimageService);
  gi.changeService(GenericInterface::WINDOW_SERVICE, eiimageService);
//  gi.addService(GenericInterface::WINDOW_SERVICE, eiimageService);

  QObject::connect(pluginManager, SIGNAL(addPlugin(OpSet*)), eiimageService, SLOT(addOpSet(OpSet*)));
  QObject::connect(pluginManager, SIGNAL(removePlugin(OpSet*)), eiimageService, SLOT(removeOpSet(OpSet*)));


   //Section to organize the display of menus, its depends the order of declaration
   //This should be define as macro for the whole program -> not possible

  QString serviceFile,opsetUtility,serviceedit,opsetimage,opsetencoding,serviceopsetMM;
  QString opsetanalysis,opsettransform,serviceopsetfiltering,opsetool;
  QString serviceplugin,opsetqm,servicewindow;
  gi.menu(serviceFile=QString(qApp->translate("menu", "&File")));
  gi.menu(opsetUtility=QString(qApp->translate("menu", "&Utility")));
  gi.menu(serviceedit=QString(qApp->translate("menu", "E&dit")));
  gi.menu(opsetimage=QString(qApp->translate("menu", "&Image")));
  gi.menu(opsetencoding=QString(qApp->translate("menu", "&Encoding")));
  gi.menu(serviceopsetMM=QString(qApp->translate("menu", "&Morpho. math.")));
  gi.menu(opsetanalysis=QString(qApp->translate("menu", "&Analysis")));
  gi.menu(opsettransform=QString(qApp->translate("menu", "&Transforms")));
  gi.menu(serviceopsetfiltering=QString(qApp->translate("menu", "&Filtering")));
  gi.menu(opsetool=QString(qApp->translate("menu", "&Tools")));
  gi.menu(serviceplugin=QString(qApp->translate("menu", "&Plugin")));
  gi.menu(servicewindow=QString(qApp->translate("menu", "&Window")));
  gi.menu(opsetqm=QString(qApp->translate("menu", "&?")));



   //Section to create the different OpSet

  BuiltinOpSet* utility= new BuiltinOpSet(opsetUtility.toStdString());
  utility->addOperation(new iwApplyMaskOp());
  utility->addOperation(new iwPixelGridOp());
  utility->addOperation(new iwCropOp());
  utility->addOperation(new iwCopyCropOp());
  utility->addOperation(new SeparatorOp());
  utility->addOperation(new siwHistogramOp());
  utility->addOperation(new siwCumulatedHistogramOp());
  utility->addOperation(new siwHProjectionHistogram());
  utility->addOperation(new siwLineProfile());
  utility->addOperation(new siwColumnProfile());
  utility->addOperation(new siwconvertToGrayscale());
  utility->addOperation(new siwconvertToBinary());

  BuiltinOpSet* image = new BuiltinOpSet(opsetimage.toStdString());
  image->addOperation(new PointOp());
  image->addOperation(new TranslateOp());
  image->addOperation(new RotateOp());
  image->addOperation(new CenterOp());
  image->addOperation(new FlipOp(FlipOp::Horizontal));
  image->addOperation(new FlipOp(FlipOp::Vertical));
  image->addOperation(new SeparatorOp());
  image->addOperation(new SplitColorOp());
  image->addOperation(new CombineColorOp());
  image->addOperation(new ScalingOp());
  image->addOperation(new QuantificationOp());
  image->addOperation(new ThresholdOp());
  image->addOperation(new HistogramOp());

  BuiltinOpSet* tools = new BuiltinOpSet(opsetool.toStdString());

  tools->addOperation(new SignalToNoiseOp());
  tools->addOperation(new MeanSquaredErrorOp());
  tools->addOperation(new EntropyOp());
  tools->addOperation(new NoiseOp());
  tools->addOperation(new SeparatorOp());
  tools->addOperation(new RandomImgOp());
  tools->addOperation(new ColorimetryOp());
  tools->addOperation(new RejectionRingOp());
  tools->addOperation(new SinusSynthesisOp());

  BuiltinOpSet* encode = new BuiltinOpSet(opsetencoding.toStdString());
  encode->addOperation(new HuffmanOp());
  encode->addOperation(new MICDEncodingOp());

  BuiltinOpSet* morpho = new BuiltinOpSet(serviceopsetMM.toStdString());
  morpho->addOperation(new DMMOp());
  morpho->addOperation(new SeparatorOp());

  BuiltinOpSet* transfo = new BuiltinOpSet(opsettransform.toStdString());
  transfo->addOperation(new FFTOp());
  transfo->addOperation(new IFFTOp());
  transfo->addOperation(new HadamardOp());
  transfo->addOperation(new DCTOp());
  transfo->addOperation(new HoughOp());
  transfo->addOperation(new InverseHoughOp());

  BuiltinOpSet* analyse = new BuiltinOpSet(opsetanalysis.toStdString());
  analyse->addOperation(new CroissanceOp());
  analyse->addOperation(new ZeroCrossingOp());
  analyse->addOperation(new PyramidOp());
  analyse->addOperation(new InversePyramidOp());
  analyse->addOperation(new ClassAnalysisOp());
  analyse->addOperation(new ClassResultOp());
  analyse->addOperation(new PseudoColorOp());

  BuiltinOpSet* filter = new BuiltinOpSet(serviceopsetfiltering.toStdString());
  filter->addOperation(new BFlitOp());

        //edit FL
  BuiltinOpSet* test = new BuiltinOpSet(serviceedit.toStdString());
  //test->addOperation(new ParamRepOp());
  test->addOperation(new iwRenameOp());
  test->addOperation(new iwZoomMoinsOp());
  test->addOperation(new iwZoomPlusOp());
  test->addOperation(new ApplyOpOp());
  test->addOperation(new ConvertOp());

  BuiltinOpSet* questionmark = new BuiltinOpSet(opsetqm.toStdString());
  questionmark->addOperation(new HelpOp());
  questionmark->addOperation(new AboutOp());



   //Section to assign the different OpSet to Services

  eiimageService->addOpSet(utility);
  eiimageService->addOpSet(test);

  eiimageService->addOpSet(image);
  eiimageService->addOpSet(encode);
  eiimageService->addOpSet(morpho);
  eiimageService->addOpSet(analyse);
  eiimageService->addOpSet(transfo);
  gi.addService(new MorphoMatService);
  gi.addService(new filtrme::FilteringService);
  eiimageService->addOpSet(filter);
  eiimageService->addOpSet(tools);
  eiimageService->addOpSet(questionmark);

  gi.addService(pluginManager);

  gi.run();


  return app.exec();

}
